{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f4970f67-e6bc-4aaf-8901-3d1f13387d11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 准备数据（简单模拟）\n",
    "import numpy as np\n",
    "\n",
    "# 模拟数据（100个样本，2个特征）\n",
    "np.random.seed(0)\n",
    "m = 100\n",
    "X = np.random.rand(m, 2)\n",
    "y = (X[:, 0] + X[:, 1] > 1).astype(int)  # 简单可分的标签\n",
    "\n",
    "# 添加一列全1（偏置项）\n",
    "X = np.c_[np.ones((m, 1)), X]\n",
    "\n",
    "# 初始化参数 θ\n",
    "theta_init = np.zeros(X.shape[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "54900906-62f5-471f-8b88-70e7f88ee690",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  2. 定义 sigmoid + costFunction（逻辑回归）\n",
    "# 作用：将任意实数 z 映射到 (0, 1) 之间。\n",
    "# 用途：逻辑回归中，我们需要输出一个概率 P(y=1|x)，这就是 hθ(x) = sigmoid(θᵀx)。\n",
    "# 图像是一个 S 型曲线\n",
    "def sigmoid(z):\n",
    "    return 1 / (1 + np.exp(-z))\n",
    "\n",
    "def costFunction_logistic(theta, X, y):\n",
    "    m = len(y)\n",
    "    # sigmoid(X @ theta) 是预测值 hθ(x)，代表 每个样本属于类别 1 的概率\n",
    "    h = sigmoid(X @ theta) \n",
    "    #print(\"X:\",X)\n",
    "    #print(\"y:\",y)\n",
    "    #print(\"h:\",h)\n",
    "    epsilon = 1e-10  # 避免 log(0)\n",
    "    cost = -(1 / m) * np.sum(y * np.log(h + epsilon) + (1 - y) * np.log(1 - h + epsilon))\n",
    "    return cost\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "75dbc6c9-ae83-4d54-8c94-5eb512ea1974",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最优参数 θ： [-3257.05546996  3255.97770914  3209.82398397]\n",
      "最小代价 J(θ)： 6.579735962148336e-06\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DELL\\AppData\\Local\\Temp\\ipykernel_136892\\3461275326.py:6: RuntimeWarning: overflow encountered in exp\n",
      "  return 1 / (1 + np.exp(-z))\n"
     ]
    }
   ],
   "source": [
    "#  3. 调用 scipy.optimize.minimize 优化 θ\n",
    "from scipy.optimize import minimize\n",
    "\n",
    "# 使用优化器寻找最优θ\n",
    "result = minimize(fun=costFunction_logistic, x0=theta_init, args=(X, y), method='BFGS')\n",
    "\n",
    "# 输出结果\n",
    "theta_opt = result.x\n",
    "print(\"最优参数 θ：\", theta_opt)\n",
    "print(\"最小代价 J(θ)：\", result.fun)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7ecf9d87-b263-430c-ae00-b291f296f4db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y_pred [ True  True  True  True  True  True  True False False  True  True  True\n",
      " False  True False  True  True False  True  True False False  True False\n",
      " False  True  True False False False False False False False False  True\n",
      "  True False False False False False False False  True False False False\n",
      " False False False  True False  True  True  True False  True  True  True\n",
      "  True  True  True False False  True False  True  True  True  True  True\n",
      "  True  True  True  True False False  True False  True  True  True False\n",
      " False False False  True  True False False False  True  True  True False\n",
      "  True  True False False]\n",
      "训练集准确率：100.00%\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DELL\\AppData\\Local\\Temp\\ipykernel_136892\\3461275326.py:6: RuntimeWarning: overflow encountered in exp\n",
      "  return 1 / (1 + np.exp(-z))\n"
     ]
    }
   ],
   "source": [
    "#  4. 用最优 θ 做预测\n",
    "def predict(X, theta):\n",
    "    return sigmoid(X @ theta) >= 0.5\n",
    "\n",
    "y_pred = predict(X, theta_opt)\n",
    "print(\"y_pred\",y_pred)\n",
    "accuracy = np.mean(y_pred == y)\n",
    "print(f\"训练集准确率：{accuracy * 100:.2f}%\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c12e292d-bb22-4d34-94eb-3ab142c15300",
   "metadata": {},
   "source": [
    "逻辑回归中的假设函数,对应sigmoid\n",
    "$$\n",
    "h_\\theta(x) = \\sigma(\\theta^T x) = \\frac{1}{1 + e^{-\\theta^T x}}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8af7d80-d8da-4747-b616-cf1b9581a64d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
